Interface (object-oriented programming)
part 2/4 Β· 9.9 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Some programming languages provide explicit language support for interfaces: Ada, C#, D, Dart, Delphi, Go, Java, Logtalk, Object Pascal, Objective-C, OCaml, PHP, Racket, Seed7, Swift, Python 3.8. In languages supporting multiple inheritance, such as C++, interfaces are implemented as abstract classes.
An example of syntax for interfaces may look like the following:
class Animal { ... }
class Theropod extends Animal { ... }
interface Flyable {
void fly();
}
interface Vocal {
void vocalize();
}
public class Bird extends Theropod implements Flyable, Vocal {
// ...
public void fly() { ... }
public void vocalize() { ... }
}
In languages without explicit support, interfaces are often still present as conventions; this is known as duck typing. For example, in Python, any class can implement an __iter__ method and be used as a collection.cite-ref-python-iter-4-0[3]
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ